local zhulian = fk.CreateSkill {
  name = "klmou__zhulian",
}

Fk:loadTranslationTable{
  ["klmou__zhulian"] = "诛连",
  [":klmou__zhulian"] = "每轮限两次，每名角色回合开始时，你可以与其各摸X张牌，若目标角色为自己，则摸X张牌，然后该回合结束时，你可以指定其弃置X种类型的所有手牌（X为该技能的发动次数且至多为3）。",
  ["@@klmou__zhulian-turn"] = "诛连",

  ["$klmou__zhulian1"] = "汝若不自招，则以诛连治罪！",
}

zhulian:addEffect(fk.TurnStart, {
  can_trigger = function(self, event, target, player, data)
    return target and player:hasSkill(zhulian.name) and player:usedEffectTimes(self.name, Player.HistoryRound) < 2
  end,
  on_cost = function(self, event, target, player, data)
    local x = math.min(player:usedEffectTimes(self.name, Player.HistoryGame), 3)
    if player.room:askToSkillInvoke(player, {
      skill_name = zhulian.name,
      prompt = "诛连：你可以与 %dest 各摸 "..x.." 张牌，然后该回合结束时你可以令其弃置该技能的发动次数（至多为3）种你指定类型的所有手牌::"..target.id,

    }) then
      event:setCostData(self, { num = x})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = event:getCostData(self).num
    if not player.dead then
      player:drawCards(num, zhulian.name)
    end
    if not target.dead and target ~= player then
      target:drawCards(num, zhulian.name)
    end
    num = math.min(player:usedEffectTimes(self.name, Player.HistoryGame), 3)
    room:setPlayerMark(target, "klmou__zhulian".. player.id, num)
    room:setPlayerMark(target, "@@klmou__zhulian-turn", 1)
  end
})

zhulian:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target and player:hasSkill(zhulian.name) and target:getMark("klmou__zhulian".. player.id) > 0
    and not target:isKongcheng() and target:getMark("@@klmou__zhulian-turn") > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local num = target:getMark("klmou__zhulian".. player.id)
    local choice = room:askToChoices(player, {
      choices = {"hx__basic", "hx__trick", "hx__equip"},
      skill_name = zhulian.name,
      min_num = num,
      max_num = num,
      cancelable = true,
      prompt = "诛连：你可以选择"..num.."种类型的手牌令 %dest 弃置::" .. target.id,
    })
    if choice then
      event:setCostData(self, { choice = choice })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    room:setPlayerMark(target, "klmou__zhulian".. player.id, 0)
    local basic = table.filter(target:getCardIds("h"), function(c) return Fk:getCardById(c).type == Card.TypeBasic end)
    local trick = table.filter(target:getCardIds("h"), function(c) return Fk:getCardById(c).type == Card.TypeTrick end)
    local equip = table.filter(target:getCardIds("h"), function(c) return Fk:getCardById(c).type == Card.TypeEquip end)
    if table.contains(choice, "hx__basic") and #basic > 0 then
      room:throwCard(basic, zhulian.name, target, target)
    end
    if table.contains(choice, "hx__trick") and #trick > 0 then
      room:throwCard(trick, zhulian.name, target, target)
    end
    if table.contains(choice, "hx__equip") and #equip > 0 then
      room:throwCard(equip, zhulian.name, target, target)
    end
  end
})


return zhulian